package com.sky.aspect;

import com.sky.context.BaseContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;

@Aspect
@Component
public class ServicePutMapperAspect {

    @Around("@annotation(com.sky.anno.ToEasyService)")
    public void toEasyService(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("------------------------------------------------------------");

        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }

        Object arg = args[0];

        //
        LocalDateTime now = LocalDateTime.now();
        Long UserId = BaseContext.getCurrentId();

        Method[] methods = arg.getClass().getMethods();

        System.out.println(arg.getClass().getName());
        System.out.println(methods.length);
        System.out.println(Arrays.toString(methods));

        for (Method method : methods) {
            if(Objects.equals(method.getName(), "setCreateTime")){
                System.out.println("方法名字："+method.getName());
                method.invoke(arg,now);

            } else if (Objects.equals(method.getName(), "setUpdateTime")) {
                method.invoke(arg,now);
            }else if (Objects.equals(method.getName(), "setUpdateUser")) {
                method.invoke(arg,UserId);
            }else if (Objects.equals(method.getName(), "setCreateUser")) {
                method.invoke(arg,UserId);
            }
        }

        joinPoint.proceed();

        System.out.println("------------------------------------------------------------");
    }
}
